package com.vue.cn.controller;

import com.vue.cn.common.http.AjaxResult;
import com.vue.cn.utils.upload.FileUtils;
import com.vue.cn.utils.upload.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("upload")
public class UploadController {

    @Value("${bucketRegion}")
    private String bucketRegion;

    /**
     * 水印设置
     */
    @Value("${style}")
    private String style;

    @Autowired
    private UploadUtils uploadUtils;

    /**
     * part方式接收文件
     */
    @PostMapping()
    public AjaxResult upload(@RequestPart("avater") Part part) throws IOException {

        String fileName = FileUtils.getFileName(part);
        uploadUtils.fileUpLoad(fileName, part);
        System.out.println(bucketRegion+fileName+style);

        return AjaxResult.ok(bucketRegion+fileName+style);
    }

    /**
     * base64接收文件
     */
    @PostMapping("base64")
    public AjaxResult strUpload(@RequestBody Map baseStr){
        String imgName = (String) baseStr.get("fileName");
        String img = (String) baseStr.get("str");

        String fileName = FileUtils.getFileName(imgName);
        byte[] content = img.getBytes();
        System.out.println(content.length);
        uploadUtils.fileUpLoad(fileName,content);
        System.out.println(bucketRegion+fileName);

        return AjaxResult.ok(img);
    }

}
